import json

import requests

from config_logger import config, log
from send_message import Send


def notify_server(ip, taskId, status, oss_url="", errorInfo = None):
    """
    通过HTTP接口通知服务器
    :param oss_url: OSS文件的URL
    """
    # 这里需要根据你的服务器API来设置请求的URL、方法和数据
    try:
        server_api_url = ip + "/api/ai/video/jianying-task"
        data = {
            "taskId": taskId,
            "videoUrl": oss_url,
            "status": status,
            "errorInfo": errorInfo}
        log.info(f"{ip}发送通知请求：{data}")
        response = requests.post(server_api_url, json=data)
        log.info(f"{ip}发送通知返回：{response.json()}")
        text = json.loads(response.text)
        if text.get("code") != 0:
            Send.qywxmsg(f'上报{ip}/api/ai/video/jianying-task返回有误：{response.text}')
    except json.JSONDecodeError as e:
        Send.qywxmsg(f'上报{ip}/api/ai/video/jianying-task返回有误：{e}')
